package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author lfh
 * @version 1.0
 * @date 2021/4/13 16:45
 */
@Api(tags = "商品SKU接口")
@RestController
@RequestMapping("admin/product")
public class SkuManageController {

    //注入服务层
    @Autowired
    private ManageService manageService;

    /**
     * 保存sku
     *
     * @param skuInfo
     * @return
     */
    //http://api.gmall.com/admin/product/saveSkuInfo
    //  获取到前端传递过来的数据！   Json --> JavaObject
    @ApiOperation(value = "保存sku")
    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        //调用服务层
        manageService.saveSkuInfo(skuInfo);
        //返回
        return Result.ok();
    }

    //http://api.gmall.com/admin/product/list/{page}/{limit}
    @ApiOperation(value = "获取sku分页列表")
    @GetMapping("list/{page}/{limit}")
    public Result getSkuInfoList(@PathVariable Long page,
                                 @PathVariable Long limit) {
        //创建一个page对象
        Page<SkuInfo> skuInfoPage = new Page<>();
        //调用服务层方法
        IPage iPage = manageService.getSkuInfoList(skuInfoPage);
        return Result.ok(iPage);
    }

    //http://api.gmall.com/admin/product/onSale/{skuId}
    @ApiOperation(value = "商品上架")
    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId) {
        //调用服务层方法
        manageService.onSale(skuId);
        //返回数据
        return Result.ok();
    }

    //http://api.gmall.com/admin/product/cancelSale/{skuId}
    @ApiOperation(value = "商品上架")
    @GetMapping("cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId) {
        //调用服务层方法
        manageService.cancelSale(skuId);
        //返回数据
        return Result.ok();
    }
}
